package com.lf.easyapi.util;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil
{
	//默认显示日期的格式
	public static final String DATAFORMAT_STR = "yyyy-MM-dd";
	
	//默认显示日期的格式
	public static final String YYYYMMDDHHMMSS_DATAFORMAT = "yyyyMMddHHmmss";
	
	//默认显示日期的格式
	public static final String YYYY_MM_DATAFORMAT_STR = "yyyy-MM";
	
	//默认显示日期时间的格式
	public static final String DATATIMEF_STR = "yyyy-MM-dd HH:mm:ss";
	
	//默认显示简体中文日期的格式
	public static final String ZHCN_DATAFORMAT_STR = "yyyy年MM月dd日";
	
	//默认显示简体中文日期时间的格式
	public static final String ZHCN_DATATIMEF_STR = "yyyy年MM月dd日HH时mm分ss秒";
	
	//默认显示简体中文日期时间的格式
	public static final String ZHCN_DATATIMEFF_STR = "yyyy年MM月dd日HH时mm分";
		
	//默认显示简体中文日期时间的格式
	public static final String ZHCN_DATATIMEF_STR_4yMMddHHmm = "yyyy年MM月dd日HH时mm分";
	
	private static DateFormat dateFormat = null;
	
	private static DateFormat dateTimeFormat = null;
	
	private static DateFormat zhcnDateFormat = null;
	
	private static DateFormat zhcnDateTimeFormat = null;
	static
	{
		dateFormat = new SimpleDateFormat(DATAFORMAT_STR);
		dateTimeFormat = new SimpleDateFormat(DATATIMEF_STR);
		zhcnDateFormat = new SimpleDateFormat(ZHCN_DATAFORMAT_STR);
		zhcnDateTimeFormat = new SimpleDateFormat(ZHCN_DATATIMEF_STR);
	}
	
	private static DateFormat getDateFormat(String formatStr)
	{
		return new SimpleDateFormat(formatStr);
	}
	
	/**
	 * 按照默认显示日期时间的格式"yyyy-MM-dd HH:mm:ss"，转化dateTimeStr为Date类型
	 * dateTimeStr必须是"yyyy-MM-dd HH:mm:ss"的形式
	 * @param dateTimeStr
	 * @return
	 */
	public static Date getDate(String dateTimeStr)
	{
		return getDate(dateTimeStr, DATATIMEF_STR);
	}
	
	/**
	 * 按照默认formatStr的格式，转化dateTimeStr为Date类型
	 * dateTimeStr必须是formatStr的形式
	 * @param dateTimeStr
	 * @param formatStr
	 * @return
	 */
	public static Date getDate(String dateTimeStr, String formatStr)
	{
		try
		{
			if (dateTimeStr == null || dateTimeStr.equals(""))
			{
				return null;
			}
			DateFormat sdf = getDateFormat(formatStr);
			java.util.Date d = sdf.parse(dateTimeStr);
			return d;
		}
		catch (ParseException e)
		{
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 将YYYYMMDD转换成Date日期
	 * @param date
	 * @return
	 * @throws BusinessException
	 */
	public static Date transferDate(String date) throws Exception
	{
		if (date == null || date.length() < 1)
			return null;
		
		if (date.length() != 8)
			throw new Exception("日期格式错误");
		String con = "-";
		
		String yyyy = date.substring(0, 4);
		String mm = date.substring(4, 6);
		String dd = date.substring(6, 8);
		
		int month = Integer.parseInt(mm);
		int day = Integer.parseInt(dd);
		if (month < 1 || month > 12 || day < 1 || day > 31)
			throw new Exception("日期格式错误");
		
		String str = yyyy + con + mm + con + dd;
		return DateUtil.getDate(str, DateUtil.DATAFORMAT_STR);
	}
	
	/**
	 * 将YYYY－MM－DD日期转换成yyyymmdd格式字符串
	 * @param date
	 * @return
	 */
	public static String getYYYYMMDDDate(Date date)
	{
		if (date == null)
			return null;
		String yyyy = getYear(date) + "";
		String mm = getMonth(date) + "";
		String dd = getDay(date) + "";
		
//		mm = StringUtil.rightAlign(mm, 2, "0");
//		dd = StringUtil.rightAlign(dd, 2, "0");
		return yyyy + mm + dd;
	}
	
	/**
	 * 将YYYY－MM－DD日期转换成YYYYMMDDHHMMSS格式字符串
	 * @param date
	 * @return
	 */
	public static String getYYYYMMDDHHMMSSDate(Date date)
	{
		if (date == null)
			return null;
		String yyyy = getYear(date) + "";
		String mm = getMonth(date) + "";
		String dd = getDay(date) + "";
		String hh = getHour(date) + "";
		String min = getMin(date) + "";
		String ss = getSecond(date) + "";
		
//		mm = StringUtil.rightAlign(mm, 2, "0");
//		dd = StringUtil.rightAlign(dd, 2, "0");
//		hh = StringUtil.rightAlign(hh, 2, "0");
//		min = StringUtil.rightAlign(min, 2, "0");
//		ss = StringUtil.rightAlign(ss, 2, "0");
		
		return yyyy + mm + dd + hh + min + ss;
	}
	
	/**
	 * 将YYYY－MM－DD日期转换成yyyymmdd格式字符串
	 * @param date
	 * @return
	 */
	public static String getYYYYMMDDDate(String date)
	{
		return getYYYYMMDDDate(getDate(date, DATAFORMAT_STR));
	}
	
	/**
	 * 将Date转换成字符串“yyyy-mm-dd hh:mm:ss”的字符串
	 * @param date
	 * @return
	 */
	public static String dateToDateString(Date date)
	{
		return dateToDateString(date, DATATIMEF_STR);
	}
	
	/**
	 * 将Date转换成formatStr格式的字符串
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String dateToDateString(Date date, String formatStr)
	{
		DateFormat df = getDateFormat(formatStr);
		return df.format(date);
	}
	
	/**
	 * 返回一个yyyy-MM-dd HH:mm:ss 形式的日期时间字符串中的HH:mm:ss
	 * @param dateTime
	 * @return
	 */
	public static String getTimeString(String dateTime)
	{
		return getTimeString(dateTime, DATATIMEF_STR);
	}
	
	/**
	 * 返回一个formatStr格式的日期时间字符串中的HH:mm:ss
	 * @param dateTime
	 * @param formatStr
	 * @return
	 */
	public static String getTimeString(String dateTime, String formatStr)
	{
		Date d = getDate(dateTime, formatStr);
		String s = dateToDateString(d);
		return s.substring(DATATIMEF_STR.indexOf('H'));
	}
	
	/**
	 * 获取当前日期yyyy-MM-dd的形式
	 * @return
	 */
	public static String getCurDate()
	{
		//return dateToDateString(new Date(),DATAFORMAT_STR);
		return dateToDateString(Calendar.getInstance().getTime(), DATAFORMAT_STR);
	}
	
	/**
	 * 获取当前日期yyyy年MM月dd日的形式
	 * @return
	 */
	public static String getCurZhCNDate()
	{
		return dateToDateString(new Date(), ZHCN_DATAFORMAT_STR);
	}
	
	/**
	 * 获取当前日期时间yyyy-MM-dd HH:mm:ss的形式
	 * @return
	 */
	public static String getCurDateTime()
	{
		return dateToDateString(new Date(), DATATIMEF_STR);
	}
	
	/**
	 * 获取当前日期时间yyyy年MM月dd日HH时mm分ss秒的形式
	 * @return
	 */
	public static String getCurZhCNDateTime()
	{
		return dateToDateString(new Date(), ZHCN_DATATIMEF_STR);
	}
	
	/**
	 * 获取当前日期时间yyyy年MM月dd日HH时mm分的形式
	 * @return
	 */
	public static String getCurZhCNTime()
	{
		return dateToDateString(new Date(), ZHCN_DATATIMEFF_STR);
	}
	
	/**
	 * 获取日期d的days天后的一个Date
	 * @param d
	 * @param days
	 * @return
	 */
	public static Date getInternalDateByDay(Date d, int days)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		now.add(Calendar.DATE, days);
		return now.getTime();
	}
	
	public static Date getInternalDateByMon(Date d, int months)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		now.add(Calendar.MONTH, months);
		return now.getTime();
	}
	
	public static Date getInternalDateByYear(Date d, int years)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		now.add(Calendar.YEAR, years);
		return now.getTime();
	}
	
	public static Date getInternalDateBySec(Date d, int sec)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		now.add(Calendar.SECOND, sec);
		return now.getTime();
	}
	
	public static Date getInternalDateByMin(Date d, int min)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		now.add(Calendar.MINUTE, min);
		return now.getTime();
	}
	
	public static Date getInternalDateByHour(Date d, int hours)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		now.add(Calendar.HOUR_OF_DAY, hours);
		return now.getTime();
	}
	
	/**
	 * 根据一个日期字符串，返回日期格式，目前支持4种
	 * 如果都不是，则返回null
	 * @param DateString
	 * @return
	 */
	public static String getFormateStr(String DateString)
	{
		String patternStr1 = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}"; //"yyyy-MM-dd"
		String patternStr2 = "[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}"; //"yyyy-MM-dd HH:mm:ss";
		String patternStr3 = "[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日";//"yyyy年MM月dd日"
		String patternStr4 = "[0-9]{4}年[0-9]{1,2}月[0-9]{1,2}日[0-9]{1,2}时[0-9]{1,2}分[0-9]{1,2}秒";//"yyyy年MM月dd日HH时mm分ss秒"
		
		Pattern p = Pattern.compile(patternStr1);
		Matcher m = p.matcher(DateString);
		boolean b = m.matches();
		if (b)
			return DATAFORMAT_STR;
		p = Pattern.compile(patternStr2);
		m = p.matcher(DateString);
		b = m.matches();
		if (b)
			return DATATIMEF_STR;
		
		p = Pattern.compile(patternStr3);
		m = p.matcher(DateString);
		b = m.matches();
		if (b)
			return ZHCN_DATAFORMAT_STR;
		
		p = Pattern.compile(patternStr4);
		m = p.matcher(DateString);
		b = m.matches();
		if (b)
			return ZHCN_DATATIMEF_STR;
		return null;
	}
	
	/**
	 * 将一个"yyyy-MM-dd HH:mm:ss"字符串，转换成"yyyy年MM月dd日HH时mm分ss秒"的字符串
	 * @param dateStr
	 * @return
	 */
	public static String getZhCNDateTime(String dateStr)
	{
		Date d = getDate(dateStr);
		return dateToDateString(d, ZHCN_DATATIMEF_STR);
	}
	
	public static String getToDateString(Date date)
	{
		return dateToDateString(date, ZHCN_DATATIMEFF_STR);
	}
	
	/**
	 * 将一个"yyyy-MM-dd"字符串，转换成"yyyy年MM月dd日"的字符串
	 * @param dateStr
	 * @return
	 */
	public static String getZhCNDate(String dateStr)
	{
		Date d = getDate(dateStr, DATAFORMAT_STR);
		return dateToDateString(d, ZHCN_DATAFORMAT_STR);
	}
	
	/**
	 * 将dateStr从fmtFrom转换到fmtTo的格式
	 * @param dateStr
	 * @param fmtFrom
	 * @param fmtTo
	 * @return
	 */
	public static String getDateStr(String dateStr, String fmtFrom, String fmtTo)
	{
		Date d = getDate(dateStr, fmtFrom);
		return dateToDateString(d, fmtTo);
	}
	
	/**
	 * 比较两个"yyyy-MM-dd HH:mm:ss"格式的日期，之间相差多少毫秒,time2-time1
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static long compareDateStr(String time1, String time2)
	{
		Date d1 = getDate(time1);
		Date d2 = getDate(time2);
		return d2.getTime() - d1.getTime();
	}
	
	/**
	 * 将小时数换算成返回以毫秒为单位的时间
	 * @param hours
	 * @return
	 */
	public static long getMicroSec(BigDecimal hours)
	{
		BigDecimal bd;
		bd = hours.multiply(new BigDecimal(3600 * 1000));
		return bd.longValue();
	}
	
	/**
	 * 获取Date中的分钟
	 * @param d
	 * @return
	 */
	public static int getMin(Date d)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		return now.get(Calendar.MINUTE);
	}
	
	/**
	 * 获取Date中的小时(24小时)
	 * @param d
	 * @return
	 */
	public static int getHour(Date d)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		return now.get(Calendar.HOUR_OF_DAY);
	}
	
	/**
	 * 获取Date中的秒
	 * @param d
	 * @return
	 */
	public static int getSecond(Date d)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		return now.get(Calendar.SECOND);
	}
	
	/**
	 * 获取xxxx-xx-xx的日
	 * @param d
	 * @return
	 */
	public static int getDay(Date d)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		return now.get(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 获取月份，1-12月
	 * @param d
	 * @return
	 */
	public static int getMonth(Date d)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		return now.get(Calendar.MONTH) + 1;
	}
	
	/**
	 * 获取19xx,20xx形式的年
	 * @param d
	 * @return
	 */
	public static int getYear(Date d)
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		now.setTime(d);
		return now.get(Calendar.YEAR);
	}
	
	/**
	 * 得到d的上个月的年份+月份,如200505
	 * @return
	 */
	public static String getYearMonthOfLastMon(Date d)
	{
		Date newdate = getInternalDateByMon(d, -1);
		String year = String.valueOf(getYear(newdate));
		String month = String.valueOf(getMonth(newdate));
		return year + month;
	}
	
	/**
	 * 得到当前日期的年和月如200509
	 * @return String
	 */
	public static String getCurYearMonth()
	{
		Calendar now = Calendar.getInstance(TimeZone.getDefault());
		String DATE_FORMAT = "yyyyMM";
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);
		sdf.setTimeZone(TimeZone.getDefault());
		return (sdf.format(now.getTime()));
	}
	
	public static Date getNextMonth(String year, String month)
	{
		String datestr = year + "-" + month + "-01";
		Date date = getDate(datestr, DATAFORMAT_STR);
		return getInternalDateByMon(date, 1);
	}
	
	public static Date getLastMonth(String year, String month)
	{
		String datestr = year + "-" + month + "-01";
		Date date = getDate(datestr, DATAFORMAT_STR);
		return getInternalDateByMon(date, -1);
	}
	
	/**
	 * 得到日期d，按照页面日期控件格式，如"2001-3-16"
	 * @param d
	 * @return
	 */
	public static String getSingleNumDate(Date d)
	{
		return dateToDateString(d, DATAFORMAT_STR);
	}
	
	/**
	 * 得到d半年前的日期,"yyyy-MM-dd"
	 * @param d
	 * @return
	 */
	public static String getHalfYearBeforeStr(Date d)
	{
		return dateToDateString(getInternalDateByMon(d, -6), DATAFORMAT_STR);
	}
	
	/**
	 * 得到当前日期D的月底的前/后若干天的时间,<0表示之前，>0表示之后
	 * @param d
	 * @param days
	 * @return
	 */
	public static String getInternalDateByLastDay(Date d, int days)
	{
		
		return dateToDateString(getInternalDateByDay(d, days), DATAFORMAT_STR);
	}
	
	/**
	 * 日期中的年月日相加
	 *  @param field int  需要加的字段  年 月 日
	 * @param amount int 加多少
	 * @return String
	 */
	public static String addDate(int field, int amount)
	{
		int temp = 0;
		if (field == 1)
		{
			temp = Calendar.YEAR;
		}
		if (field == 2)
		{
			temp = Calendar.MONTH;
		}
		if (field == 3)
		{
			temp = Calendar.DATE;
		}
		
		String Time = "";
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance(TimeZone.getDefault());
			cal.add(temp, amount);
			Time = sdf.format(cal.getTime());
			return Time;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
		
	}
	
	/**
	 * 获得系统当前月份的天数
	 * @return
	 */
	public static int getCurentMonthDay()
	{
		Date date = Calendar.getInstance().getTime();
		return getMonthDay(date);
	}
	
	/**
	 * 获得指定日期月份的天数
	 * @return
	 */
	public static int getMonthDay(Date date)
	{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getActualMaximum(Calendar.DAY_OF_MONTH);
		
	}
	
	/**
	 * 获得指定日期月份的天数  yyyy-mm-dd
	 * @return
	 */
	public static int getMonthDay(String date)
	{
		Date strDate = getDate(date, DATAFORMAT_STR);
		return getMonthDay(strDate);
		
	}
	
	public static String getStringDate(Calendar cal)
	{
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(cal.getTime());
	}
	
	/**
	 * 将一个"yyyyMMddhhmmss"字符串，转换成"yyyy年MM月dd日"的字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getZhStrCNDate(String dateStr) {
		Date d = getDate(dateStr, YYYYMMDDHHMMSS_DATAFORMAT);
		return dateToDateString(d, ZHCN_DATAFORMAT_STR);
	}
	
	/**
	 * 将一个"yyyyMMddhhmmss"字符串，转换成"yyyy-MM-dd"的字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getDataFormatStr(String dateStr) {
		Date d = getDate(dateStr, YYYYMMDDHHMMSS_DATAFORMAT);
		return dateToDateString(d, DATAFORMAT_STR);
	}
	
	/**
	 * 将一个"yyyyMMddhhmmss"字符串，转换成"yyyy-MM-dd HH:mm:ss"的字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String getDataTimefStr(String dateStr) {
		Date d = getDate(dateStr, YYYYMMDDHHMMSS_DATAFORMAT);
		return dateToDateString(d, DATATIMEF_STR);
	}
	
	/**
	 * 将一个  yyyy-MM-dd HH:mm:ss 字符串，转换成"yyyyMMddhhmmss"的字符串
	 * 
	 * @param dateStr
	 * @return
	 */
	public static String DataTimefStr(String dateStr) {
		Date d = getDate(dateStr, DATATIMEF_STR);
		return dateToDateString(d, YYYYMMDDHHMMSS_DATAFORMAT);
	}
	
	/**
	 * 获取前一个的月份
	 * @return
	 */
	public static String getBeforeMonth() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date = new SimpleDateFormat("yyyyMM").format(c.getTime());
		return date;
	}
	
	/**
	 * 获取后一个的月份
	 * @return
	 */
	public static String getNextMonth() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, +1);
		String date = new SimpleDateFormat("yyyyMM").format(c.getTime());
		return date;
	}
	
	/**
	 * 获取后一天
	 * @return
	 */
	public static String getNextDayTime(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String reStr = "";
				
		try {
			Date dt = sdf.parse(date);
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(dt);
			rightNow.add(Calendar.DAY_OF_YEAR, 1);// 日期加1天
			Date dt1 = rightNow.getTime();
			reStr = sdf.format(dt1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return reStr;
	}
	
	/**
	 * 获取后二天
	 * @return
	 */
	public static String getNextTwoDayTime(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String reStr = "";
				
		try {
			Date dt = sdf.parse(date);
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(dt);
			rightNow.add(Calendar.DAY_OF_YEAR, 2);// 日期加1天
			Date dt1 = rightNow.getTime();
			reStr = sdf.format(dt1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return reStr;
	}
	
	
	/**
	 * 比较时间大小，大于返回false，小于返回true
	 * @param date1 当前时间
	 * @param date2 比较时间
	 * @return
	 */
	public static boolean checkCurrentTime(String date1,String date2,String format) {
		try {
			SimpleDateFormat df = new SimpleDateFormat(format);
			return df.parse(date1).before(df.parse(date2));
		} catch (ParseException e) {
//			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 小数点后 保留两位
	 * @param number
	 * @return
	 */
	public static String keepTwoBit(String number) {
		DecimalFormat myFormatter  = new DecimalFormat("###.00");// 保留2位
		return myFormatter.format(Double.parseDouble(number));
	}
	
	/**
	 * 时间差（返回分钟）
	 * @param time
	 * @return
	 */
	public static Long difTimeForMin(Date time) {
//		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		long minutes = 0;
		try {
			Date now = new Date();
//			Date d1 = df.parse(time);
			long diff = now.getTime() - time.getTime();
			minutes = diff / (1000 * 60);
		} catch (Exception e) {
			// TODO: handle exception
		}

		return minutes;
	}
	
	/**
	 * 用于流量不清零判断 是否有效
	 * @param date1 生效时间
	 * @param date2 失效时间
	 * @return
	 */
	public static boolean compare_date(String date1, String date2) {
    	SimpleDateFormat df = null;
    	if (!date1.isEmpty() && !date2.isEmpty()) {
    		if (8 == date1.trim().length()) {
    			df = new SimpleDateFormat("yyyyMMdd");
    		} else {
    			df = new SimpleDateFormat("yyyyMMddHHmmss");
    		}
    	}
		try {
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			String now = df.format(new Date());
			Date now1 = df.parse(now); // 当前时间

			// EFF_TIME≤sys_date＜EXP_TIME，表示该流量当前有效
			int compare1 = now1.compareTo(dt1);
			int compare2 = dt2.compareTo(now1);
			if ((compare1 == 1 || compare1 == 0) && compare2 == 1) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 流量专区仪表盘标记位
	 * @return 0表示默认展示日间流量   1表示默认展示夜间流量
	 */
	public static String checkTime(){
		String flag = "0";
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		if(hour<=7||hour>=23){
			flag = "1";
		}
		return flag;
	}
	
	 /**
     * 
     * 计算两个日期相差的月份数
     * 
     * @param date1 日期1
     * @param date2 日期2
     * @param pattern  日期1和日期2的日期格式
     * @return  相差的月份数
     * @throws ParseException
     */
    public static int countMonths(Calendar c1,Calendar c2/**,String pattern*/) throws ParseException{
//        SimpleDateFormat sdf=new SimpleDateFormat(pattern);
//        Calendar c1=Calendar.getInstance();
//        Calendar c2=Calendar.getInstance();
//        c1.setTime(sdf.parse(date1));
//        c2.setTime(sdf.parse(date2));
        
        int year =c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR);
        
        //开始日期若小月结束日期
        if(year<0){
            year=-year;
            return year*12+c1.get(Calendar.MONTH)-c2.get(Calendar.MONTH);
        }
       
        return year*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
    }

	/**
	 * 获取当天的周天数（例如：周日）
	 * @param day为Calendar.get(Calendar.DAY_OF_WEEK)常量值
	 * @return
	 */
	private static String getDay(int day){
		String today="";
		switch (day) {
			case Calendar.MONDAY:
				today="周一";
				break;
			case Calendar.TUESDAY:
				today="周二";
				break;
			case Calendar.WEDNESDAY:
				today="周三";
				break;
			case Calendar.THURSDAY:
				today="周四";
				break;
			case Calendar.FRIDAY:
				today="周五";
				break;
			case Calendar.SATURDAY:
				today="周六";
				break;
			case Calendar.SUNDAY:
				today="周日";
				break;
		}
		return today;
	}

	/**
	 * 判断当前时间是否是营业厅营业时间
	 * @param 营业厅营业时间  time 格式为：new String[]{"周一、周二、周三、周四、周五、周六、周日","08:00-12:00","12:00-17:00"}
	 * @return
	 * @throws ParseException
	 */
	public static boolean checkTime(String[] time) throws ParseException{
		if(time!=null&&time.length>=3){
			Calendar date=Calendar.getInstance();
			String day=time[0];
			String amTime=time[1];
			String pmTime=time[2];
			//today  周天数 ： 周一~周日
			String today=getDay(date.get(Calendar.DAY_OF_WEEK));
			//当天为营业厅营业日
			if(!"".equals(today)&&day.indexOf(today)!=-1){
				return ifWorkingTime(amTime)||ifWorkingTime(pmTime);
			}
		}
		return false;
	}

	/**
 * 判断当天时间是否在时间段内
 * @param timeZone  格式例如：    8:00-12:00
 * @return
 * @throws ParseException
 */
public static boolean ifWorkingTime(String timeZone) throws ParseException{
	if(timeZone!=null&&!"".equals(timeZone)&&timeZone.indexOf("-")!=-1){
		SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd ");
		SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date nowTime=new Date();
		String preStr=sdf1.format(nowTime);
		String[] times=timeZone.split("-");
		Date mixTime=sdf2.parse(preStr+times[0]);
		Date maxTime=sdf2.parse(preStr+times[1]);
		if(mixTime.getTime()<=nowTime.getTime()&&maxTime.getTime()>=nowTime.getTime()){
			return true;
		}
	}
	return false;
}

	/**
	 * 判断当前时间是否是营业厅结束时间半小时前
	 * @param 营业厅营业时间  time 格式为：new String[]{"周一、周二、周三、周四、周五、周六、周日","08:00-12:00","12:00-17:00"}
	 * @return
	 * @throws ParseException
	 */
	public static boolean checkBeforeTime(String[] time) throws ParseException{
		if(time!=null&&time.length>=3){
			Calendar date=Calendar.getInstance();
			String day=time[0];
			String amTime=time[1];
			String pmTime=time[2];
			//today  周天数 ： 周一~周日
			String today=getDay(date.get(Calendar.DAY_OF_WEEK));
			//当天为营业厅营业日
			if(!"".equals(today)&&day.indexOf(today)!=-1){
//				return beforeWorkingTime(amTime)||beforeWorkingTime(pmTime);
				//判断是否在营业厅上午结束时间半小时内，如果在半小时内，则返回false
				if(beforeWorkingTime(amTime)||beforeWorkingTime(pmTime)){
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * 判断当前时间是否在结束时间半小时之内
	 * @param timeZone  格式例如：    8:00-12:00
	 * @return
	 * @throws ParseException
	 */
	public static boolean beforeWorkingTime(String timeZone) throws ParseException{
		if(timeZone!=null&&!"".equals(timeZone)&&timeZone.indexOf("-")!=-1){
			SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd ");
			SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			Date nowTime=new Date();
			String preStr=sdf1.format(nowTime);
			String[] times=timeZone.split("-");
			Date maxTime=sdf2.parse(preStr+times[1]);
			if(maxTime.getTime() >= nowTime.getTime() && maxTime.getTime() <= (nowTime.getTime()+30*60*1000)){
				return true;
			}
		}
		return false;
	}
    
	
    /**
     * 获取某月的第一天
     * @param date
     * @param farmat_str
     * @return
     */
    public static String getFirstDay(Date date,String farmat_str){
        //创建日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = dateToDateString(calendar.getTime(),farmat_str);
        return day_first;
    }

    /**
     * 获取某月的最后一天
     * @param date
     * @param farmat_str
     * @return
     */
    public static String getLastDay(Date date,String farmat_str){
        //创建日历
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);    //加一个月
        calendar.set(Calendar.DATE, 1);     //设置为该月第一天
        calendar.add(Calendar.DATE, -1);    //再减一天即为上个月最后一天
        String day_last = dateToDateString(calendar.getTime(),farmat_str);
        return day_last;
    }

	/**
	 * 获取下月的第一天
	 * @param date
	 * @param farmat_str
	 * @return
	 */
	public static String getNextMonthFirstDay(String farmat_str){
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, +1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		String day_first = dateToDateString(c.getTime(),farmat_str);
		return day_first;
	}
    
    public static void main(String[] args) {
    	Calendar c = Calendar.getInstance();
		c.setTime(DateUtil.getDate("20170523002300", DateUtil.YYYYMMDDHHMMSS_DATAFORMAT));
		System.out.println(dayForWeek("20170508002300"));
		
	}
	
	/**
	 * 判断当前日期是星期几
	 * java ： 2,3,4,5,6,7,1  分别对应 周一到周日
	 * 该方法返回 ：1,2,3,4,5,6,7 分别对应 周一到周日
	 * @param String time
	 * @return
	 */
	public static int dayForWeek(String time) {
		int dayForWeek = 0;
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(getDate(time, YYYYMMDDHHMMSS_DATAFORMAT));
			if (c.get(Calendar.DAY_OF_WEEK) == 1) {
				dayForWeek = 7;
			} else {
				dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
			}
		} catch (Exception e) {
			
		}
		return dayForWeek;
	}
	
}